package com.example.demo.mapper;

import com.example.demo.dto.ChatMessageDTO;
import com.example.demo.entity.ChatMessage;
import org.apache.ibatis.annotations.*;

import java.time.Instant;
import java.util.List;

@Mapper
public interface ChatMessageMapper {
    // 保存消息
    @Insert("INSERT INTO chat_messages(sender, user_id, content, created_at,type) " +
            "VALUES(#{sender}, #{userId}, #{content}, #{timeStamp},#{type})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertMessage(ChatMessage message);

    // 查询活跃用户ID列表
    @Select("SELECT DISTINCT user_id FROM chat_messages " +
            "WHERE chat_messages.created_at > #{cutoff}")
    List<String> selectActiveUserIds(Instant cutoff);

    // 查询用户历史消息
    @Results({
            @Result(column = "created_at",property = "timeStamp")
    })
    @Select("SELECT * FROM chat_messages " +
            "WHERE user_id = #{userId} ORDER BY created_at ")
    List<ChatMessageDTO> selectMessagesByUserId(String userId);
}
